今天是一個小Tip的日子,當我們在座每項測試案例時,不可能每次都要包Provider吧 太累
import { render } from '@testing-library/react';
import { UserProvider } from '@contexts/nameContext';
const renderWithProvider = (ui: any, opt: any) =>
render(ui, {
wrapper: UserProvider,
...opt
});
export * from '@testing-library/react';
export { renderWithProvider as render };
所以我們可以用一個utils把他包起來
import { render, screen } from '../../../test-utils/context-utils';
import Name from '../Name';
test.only('should get name from context', async () => {
render(<Name />, {});
const getName = screen.queryByText(/hello/i);
expect(getName).toBeTruthy();
});
這樣我們隨時都可以使用Provider的狀態